package cn.com.wxd.biz.website;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.util.SpringContextHolder;
import cn.com.wxd.util.website.WebsiteInfo;
/**
 * 评论的缓存工具类
 * @author wangxudong
 *
 */
public class CommentCache {
	private static final Logger log = LoggerFactory.getLogger(CommentCache.class);
	private static CommentService commentService;
	private static CommentCache commentCache;
	private CommentCache(){
	}
	public static synchronized CommentCache getInstance(){
		if(commentCache ==null){
			commentCache = new CommentCache();
		}
		if(commentService==null){
			commentService = SpringContextHolder.getBean("commentService",CommentService.class);
			if(commentService==null){
				log.error("Spring持有类中无法获取到CommentService实例!",new Exception("Spring持有类中无法获取到CommentService实例!"));
				return null;
			}
		}
		return commentCache;
	}
	/**
	 * 添加一个评论的点赞到缓存如果超过累计超过10个。则归档到数据库
	 * @param commentId
	 * @param count 数量
	 * @return
	 */
	public boolean addPraiseToCache(int commentId,int count) {
		if(WebsiteInfo.isCache()){
			HashMap<Integer,Integer> praiss = (HashMap<Integer,Integer>)WebsiteCache.getNoRefreshCache("praiss");
			if(praiss==null){
				praiss = new HashMap<>();
			}
			if(praiss.get(commentId)!=null){
				praiss.put(commentId,praiss.get(commentId)+count);
			}else{
				praiss.put(commentId,count);
			}
			/**
			 * 只要有一个的赞超过10或者最大缓存数量个文章被点了，则刷新所有的赞数量到数据库
			 */
			if(praiss.get(commentId)>=10||praiss.size()>WebsiteInfo.getCacheMaxCount()){
				if(log.isDebugEnabled()){
					log.debug("点赞数缓存中大于10进行归档到数据库");
				}
				int[] commids = new int[praiss.size()];
				int[] prais= new int[praiss.size()];
				int i = 0;
				for (Integer key : praiss.keySet()) {
					commids[i] = key;
					prais[i] = praiss.get(key);
					i++;
				}
				if(commentService.addPraiseToDb(commids, prais)){
					praiss.clear();//清理所有的赞操作
				}
			}
			WebsiteCache.setNoRefreshCache("praiss", praiss);
			return true;
		}else{
			if(commentService.addPraiseToDb(new int[]{commentId}, new int[]{1})){
				return true;
			}
		}
		return false;
	}
	/**
	 * 添加一个评论的不喜欢到缓存如果超过累计超过10个。则归档到数据库
	 * @param commentId
	 * @param count 数量
	 * @return
	 */
	public boolean addDislikeToCache(int commentId,int count) {
		if(WebsiteInfo.isCache()){
			HashMap<Integer,Integer> dislike = (HashMap<Integer,Integer>)WebsiteCache.getNoRefreshCache("dislike");
			if(dislike==null){
				dislike = new HashMap<>();
			}
			if(dislike.get(commentId)!=null){
				dislike.put(commentId,dislike.get(commentId)+count);
			}else{
				dislike.put(commentId,count);
			}
			/**
			 * 只要有一个的不喜欢超过10或者最大缓存数量个文章被点了，则刷新所有的不喜欢数量到数据库
			 */
			if(dislike.get(commentId)>=10||dislike.size()>WebsiteInfo.getCacheMaxCount()){
				if(log.isDebugEnabled()){
					log.debug("不喜欢数缓存中大于10进行归档到数据库");
				}
				int[] commids = new int[dislike.size()];
				int[] dislikes= new int[dislike.size()];
				int i = 0;
				for (Integer key : dislike.keySet()) {
					commids[i] = key;
					dislikes[i] = dislike.get(key);
					i++;
				}
				if(commentService.addDislikeToDb(commids, dislikes)){
					dislike.clear(); //清理所有的不喜欢缓存
				}
			}
			WebsiteCache.setNoRefreshCache("dislike", dislike);
			return true;
		}else{
			if(commentService.addDislikeToDb(new int[]{commentId}, new int[]{1})){
				return true;
			}
		}
		return false;
	}
	/**
	 * 添加一个评论的举报到缓存如果超过累计超过5个。则归档到数据库
	 * @param commentId
	 * @param count 数量
	 * @return
	 */
	public boolean addReportToCache(int commentId,int count) {
		if(WebsiteInfo.isCache()){
			HashMap<Integer,Integer> report = (HashMap<Integer,Integer>)WebsiteCache.getNoRefreshCache("commentReport");
			if(report==null){
				report = new HashMap<>();
			}
			if(report.get(commentId)!=null){
				report.put(commentId,report.get(commentId)+count);
			}else{
				report.put(commentId,count);
			}
			/*
			 * 只要有一个的举报数量大于等于5 就同步到数据库
			 */
			if(report.get(commentId)>=5||report.size()>WebsiteInfo.getCacheMaxCount()){
				if(log.isDebugEnabled()){
					log.debug("举报树缓存中大于5进行归档到数据库");
				}
				int[] commids = new int[report.size()];
				int[] dislikes= new int[report.size()];
				int i = 0;
				for (Integer key : report.keySet()) {
					commids[i] = key;
					dislikes[i] = report.get(key);
					i++;
				}
				if(commentService.addReportToDb(commids, dislikes)){
					report.clear(); //清理所有的举报
				}
			}
			WebsiteCache.setNoRefreshCache("commentReport", report);
			return true;
		}else{
			if(commentService.addReportToDb(new int[]{commentId}, new int[]{1})){
				return true;
			}
		}
		return false;
	}
	/**
	 * 获取缓存中没有归档的点赞数
	 * @param commId
	 * @return
	 */
	public int getCachePraiseCount(int commId) {
		HashMap<Integer,Integer> praiss = (HashMap<Integer,Integer>)WebsiteCache.getNoRefreshCache("praiss");
		if(praiss==null){
			praiss = new HashMap<>();
		}
		if(praiss.get(commId)!=null){
			return praiss.get(commId);
		}else{
			return 0;
		}
	}
	/**
	 * 获取缓存中没有归档的不喜欢数
	 * @param commId
	 * @return
	 */
	public int getCacheDislikeCount(int commId) {
		HashMap<Integer,Integer> dislike = (HashMap<Integer,Integer>)WebsiteCache.getNoRefreshCache("dislike");
		if(dislike==null){
			dislike = new HashMap<>();
		}
		if(dislike.get(commId)!=null){
			return dislike.get(commId);
		}else{
			return 0;
		}
	}
	/**
	 * 获取缓存中没有归档的举报数
	 * @param commId
	 * @return
	 */
	public int getCacheReportCount(int commId) {
		HashMap<Integer,Integer> report = (HashMap<Integer,Integer>)WebsiteCache.getNoRefreshCache("commentReport");
		if(report==null){
			report = new HashMap<>();
		}
		if(report.get(commId)!=null){
			return report.get(commId);
		}else{
			return 0;
		}
	}
}
